package com.powerflasher.as3potrace.geom
{
   import flash.geom.Point;
   
   public class CubicCurve
   {
       
      
      private var xPen:Number;
      
      private var yPen:Number;
      
      private var _result:Vector.<Vector.<Point>>;
      
      public function CubicCurve()
      {
         super();
      }
      
      private function intersect2Lines(param1:Point, param2:Point, param3:Point, param4:Point) : Point
      {
         var _loc5_:Number = param1.x;
         var _loc6_:Number = param1.y;
         var _loc7_:Number = param4.x;
         var _loc8_:Number = param4.y;
         var _loc9_:Number = param2.x - _loc5_;
         var _loc10_:Number = param3.x - _loc7_;
         if(_loc9_ == 0 && _loc10_ == 0)
         {
            return null;
         }
         var _loc11_:Number = (param2.y - _loc6_) / _loc9_;
         var _loc12_:Number = (param3.y - _loc8_) / _loc10_;
         if(_loc9_ == 0)
         {
            return new Point(_loc5_,_loc12_ * (_loc5_ - _loc7_) + _loc8_);
         }
         if(_loc10_ == 0)
         {
            return new Point(_loc7_,_loc11_ * (_loc7_ - _loc5_) + _loc6_);
         }
         var _loc13_:Number = (-_loc12_ * _loc7_ + _loc8_ + _loc11_ * _loc5_ - _loc6_) / (_loc11_ - _loc12_);
         var _loc14_:Number = _loc11_ * (_loc13_ - _loc5_) + _loc6_;
         return new Point(_loc13_,_loc14_);
      }
      
      private function midLine(param1:Point, param2:Point) : Point
      {
         return new Point((param1.x + param2.x) / 2,(param1.y + param2.y) / 2);
      }
      
      private function bezierSplit(param1:Point, param2:Point, param3:Point, param4:Point) : Vector.<Point>
      {
         var _loc5_:Function = this.midLine;
         var _loc6_:Point = _loc5_(param1,param2);
         var _loc7_:Point = _loc5_(param2,param3);
         var _loc8_:Point = _loc5_(param3,param4);
         var _loc9_:Point = _loc5_(_loc6_,_loc7_);
         var _loc10_:Point = _loc5_(_loc7_,_loc8_);
         var _loc11_:Point = _loc5_(_loc9_,_loc10_);
         var _loc12_:Vector.<Point> = new Vector.<Point>(8);
         _loc12_[0] = param1.clone();
         _loc12_[1] = _loc6_;
         _loc12_[2] = _loc9_;
         _loc12_[3] = _loc11_;
         _loc12_[4] = _loc11_;
         _loc12_[5] = _loc10_;
         _loc12_[6] = _loc8_;
         _loc12_[7] = param4.clone();
         return _loc12_;
      }
      
      private function cBez(param1:Point, param2:Point, param3:Point, param4:Point, param5:Number) : void
      {
         var _loc9_:Vector.<Point> = null;
         var _loc10_:Vector.<Point> = null;
         var _loc6_:Point = this.intersect2Lines(param1,param2,param3,param4);
         if(_loc6_ == null)
         {
            return;
         }
         var _loc7_:Number = (param1.x + param4.x + _loc6_.x * 4 - (param2.x + param3.x) * 3) * 0.125;
         var _loc8_:Number = (param1.y + param4.y + _loc6_.y * 4 - (param2.y + param3.y) * 3) * 0.125;
         if(_loc7_ * _loc7_ + _loc8_ * _loc8_ > param5)
         {
            _loc9_ = this.bezierSplit(param1,param2,param3,param4);
            this.cBez(param1,_loc9_[1],_loc9_[2],_loc9_[3],param5);
            this.cBez(_loc9_[4],_loc9_[5],_loc9_[6],param4,param5);
         }
         else
         {
            _loc10_ = new Vector.<Point>(3);
            _loc10_[0] = new Point(this.xPen,this.yPen);
            _loc10_[1] = _loc6_.clone();
            _loc10_[2] = param4.clone();
            this.result.push(_loc10_);
            this.xPen = param4.x;
            this.yPen = param4.y;
         }
      }
      
      public function drawBezierPts(param1:Point, param2:Point, param3:Point, param4:Point, param5:Number = -1) : void
      {
         if(param5 <= 0)
         {
            param5 = 5;
         }
         this.xPen = param1.x;
         this.yPen = param1.y;
         this._result = new Vector.<Vector.<Point>>();
         this.cBez(param1,param2,param3,param4,param5 * param5);
      }
      
      public function drawBezier(param1:Number, param2:Number, param3:Number, param4:Number, param5:Number, param6:Number, param7:Number, param8:Number, param9:Number = -1) : void
      {
         this.drawBezierPts(new Point(param1,param2),new Point(param3,param4),new Point(param5,param6),new Point(param7,param8),param9);
      }
      
      public function curveToCubicPts(param1:Point, param2:Point, param3:Point, param4:Number = -1) : void
      {
         if(param4 <= 0)
         {
            param4 = 5;
         }
         this._result = new Vector.<Vector.<Point>>();
         this.cBez(new Point(this.xPen,this.yPen),param1,param2,param3,param4 * param4);
      }
      
      public function curveToCubic(param1:Number, param2:Number, param3:Number, param4:Number, param5:Number, param6:Number, param7:Number = -1) : void
      {
         this.curveToCubicPts(new Point(param1,param2),new Point(param3,param4),new Point(param5,param6),param7);
      }
      
      public function get result() : Vector.<Vector.<Point>>
      {
         return this._result;
      }
   }
}
